package youcheng.work.view_zhexian;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class DeviceAdapter extends BaseAdapter {

    private List<BluetoothDevice> mData;
    private Context mContext;

    public DeviceAdapter(List<BluetoothDevice> data, Context context){
        mData= data;
        mContext = context.getApplicationContext();
    }
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView= convertView;
        if(itemView == null){
            /* 你的xml的最外层布局的一些对于其父布局的一些诉求属性，不管怎么设置都不起作用*/
            itemView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_2, parent, false);
        }
        //第一行是名称 第二行是地址
        TextView line1 = (TextView) itemView.findViewById(android.R.id.text1);
        TextView line2 = (TextView) itemView.findViewById(android.R.id.text2);

        //获取对应蓝牙设备
        BluetoothDevice device = (BluetoothDevice) getItem(position);

        //名称
        line1.setText(device.getName());
        //地址
        line2.setText(device.getAddress());

        return itemView;

    }

    //刷新数据用的
    public void refresh(List<BluetoothDevice> data) {
        mData = data;
        /* 有时候我们需要修改已经生成的列表，添加或者修改数据，notifyDataSetChanged()可以在修改适配器绑定的数组后，不用重新刷新Activity，通知Activity更新ListView。*/
        notifyDataSetChanged();
        Log.i("TAG","hhhh");
    }
}
